<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class user extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        parent::checkLogin();

    }

//玩家信息
    function playerlist()
    {

        $DB2 = $this->load->database('account', true);


        $array = array();
//        $array = array("invalid" => 0);
        $result = $DB2
            ->select('UserID,Accounts,NickName,RegisterDate,Gender,RoomCard,isBuy')
            ->from('AccountsInfo')
            ->join('QPAdminDB.dbo.sys_user aa', ' AccountsInfo.UserID=aa.uid ', 'left')
            ->where($array)
            ->get()
            ->result_array();

        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('playerlist.html', $data);
        $this->load->view('footer.html');


    }

//代理信息

    function agentlist()
    {
        if (isset($_REQUEST['state'])) {
            if ($_REQUEST['state'] != '') {
                $array = array("invalid" => $_REQUEST['state']);
            } else {
                $array = array();
            }
        } else {
            $array = array();
        }
        $result = $this->db
            ->select('id,nickname,phone,invitecode,regtime,cardcount,lid,invalid')
            ->where($array)
            ->get('Record_Member')->result_array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('agentlist.html');
        $this->load->view('footer.html');
    }

    //代理编辑页面
    public function editagentpage()
    {
        $id = $this->uri->segment(3);
//        $pagename = $this->uri->segment(4);

        $result = array('id' => $id);
        $data['data'] = array($result, $_SESSION);

        $this->load->view('editpage.html', $data);
    }

//代理等级页面
    function agentLevelList()
    {
        $array = array();
//        $array = array("invalid" => 0);
//        $result = $this->db
//            ->select('aa.id,aa.level,aa.time,bb.level rLevel,bb.rebate_rate')
//            ->from('sys_agent_level aa')
//            ->join('sys_rebate_level bb', 'aa.srs_level = bb.id', 'left')
//            ->where($array)
//            ->get();


        $sql = 'SELECT COUNT(tt.lid) AS lidNumber, dd.* FROM (SELECT aa.id, aa.level, aa.time, bb.level AS "rLevel", bb.rebate_rate FROM "sys_agent_level" "aa" LEFT JOIN "sys_rebate_level" "bb" ON aa.srs_level = bb.id ) dd LEFT JOIN Record_Member tt ON dd.id = tt.lid GROUP BY tt.lid, dd.id, dd.LEVEL, dd.TIME, dd.rLevel, dd.rebate_rate';

        $result = $this->db
            ->query($sql)
            ->result_array();
//        var_dump($result);
//        die;

        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('agentlevellist.html');
        $this->load->view('footer.html');
    }

//新增代理等级
    function addAgentLevel()
    {
        $level = $this->input->post('level');
        $srs_level = $this->input->post('rLevel');
//        $name = $this->input->post('des');
        $array = array(
            'level' => $level,
            "srs_level" => $srs_level,
        );
        //  'time'=>time()
        $ret = $this->db->where($array)->get('sys_agent_level')->result_array();
        if ($ret) {
            $this->alert_message(0, '代理等级已经存在', site_url('user/agentLevelList'));
            exit;
        }
        $array['time'] = time();
        $ret = $this->db->insert('sys_agent_level', $array);
        if ($ret) {
            $this->alert_message(1, '添加成功', site_url('user/agentLevelList'));
        } else {
            $this->alert_message(0, '添加失败', site_url('user/agentLevelList'));
        }

    }

//配置代理等级
    function editAgentLevel()
    {
        $id = $this->uri->segment(3);

        $array = array('id' => $id);

        $result = $this->db
            ->select('level,id')
            ->where($array)
            ->get('sys_agent_level')->result_array();

        $levelResult = $this->db
            ->select('id,level,rebate_rate')
            ->where('type=1')
            ->get('sys_rebate_level')->result_array();

        $data['data'] = array($result, $_SESSION, $levelResult);

        $this->load->view('head.html', $data);
        $this->load->view('editagentlevel.html');
        $this->load->view('footer.html');

    }

    function doEditAgentLevel()
    {
        $id = $this->input->post('id');
//        $level = $this->input->post('level');
        $srs_level = $this->input->post('rLevel');


        $array = array(
            'srs_level' => $srs_level,
            'time' => time()
        );
        $ret = $this->db->where('id', $id)->update('sys_agent_level', $array);
        if ($ret) {
            $this->alert_message(1, '配置成功', site_url('user/agentLevelList'));
        } else {
            $this->alert_message(0, '配置失败', site_url('user/agentLevelList'));
        }
    }

    function editAgent()
    {
        $id = $this->uri->segment(3);
        $name = $this->uri->segment(4);

        $result = array('id' => $id,
            'name' => $name
        );
        $levelResult = $this->db
            ->select('level,id')
            ->get('sys_agent_level')->result_array();
        $data['data'] = array($result, $_SESSION, $levelResult);

        $this->load->view('head.html', $data);
        $this->load->view('editagent.html');
        $this->load->view('footer.html');

    }

    function doEditAgent()
    {
        $id = $this->input->post('id');
        $srs_level = $this->input->post('Level');

        $array = array(
            'lid' => $srs_level,
        );

        $ret = $this->db->where('id', $id)->update('Record_Member', $array);
        if ($ret) {
            $this->alert_message(1, '配置成功', site_url('user/agentlist'));
        } else {
            $this->alert_message(0, '配置失败', site_url('user/agentlist'));
        }
    }

//用于异步获取返利相关信息
    function getSrsRate()
    {
        $id = $this->input->post('name');

        $result = $this->db
            ->select('bb.level rLevel,bb.rebate_rate')
            ->from('sys_agent_level aa')
            ->join('sys_rebate_level bb', 'aa.srs_level = bb.id', 'left')
            ->where('aa.id', $id)
            ->get()
            ->result_array();

        echo json_encode($result[0]);
    }

    function setUserState()
    {
        $id = $this->uri->segment(3);
//        $pagename = $this->uri->segment(4);

        $array = array('isBuy' => 1);


        $ret = $this->db->where('uid', $id)->update('sys_user', $array);
        if ($ret) {
            $this->alert_message(1, '修改成功', site_url('user/playerlist'));
        } else {
            $this->alert_message(0, '修改失败', site_url('user/playerlist'));
        }
    }

//删除代理用户信息
    function doDelAgent()
    {
        $id = $this->uri->segment(3);
        $type = $this->uri->segment(4);

        if ($type == 'del') {
            $array = array(
                'invalid' => 1
            );
        } else {
            $array = array(
                'invalid' => 0
            );
        }


        $ret = $this->db->where('id', $id)->update('Record_Member', $array);

        if ($ret) {
            if ($type == 'del') {
                $this->alert_message(1, '删除成功', site_url('user/agentlist'));
            } else {
                $this->alert_message(1, '恢复成功', site_url('user/agentlist'));
            }
        } else {
            if ($type == 'del') {
                $this->alert_message(0, '删除失败', site_url('user/agentlist'));
            } else {
                $this->alert_message(0, '恢复失败', site_url('user/agentlist'));
            }
        }
    }


    function getAllCounts()
    {

    }
}